using System.Collections;
using UnityEngine;

public class TRB_AfterProjectOutsideCutscene : MonoBehaviour
{
	[Header("-= Cutscene References =-")]
	[SerializeField]
	private CameraManager playerCamera;

	[SerializeField]
	private Animator Toriel;

	[SerializeField]
	private Animator Alphys;

	[SerializeField]
	private Animator Berdly;

	[SerializeField]
	private GameObject BerdlySweatOverlay;

	[SerializeField]
	private Animator Susie;

	[SerializeField]
	private PlayerManager Kris;

	[SerializeField]
	private PartyMember PartyMember_Berdly;

	[Header("- Dialogue -")]
	[SerializeField]
	private CHATBOXTEXT[] CutsceneChats;

	[Header("- Sounds -")]
	[SerializeField]
	private AudioClip[] CutsceneSounds;

	private void Start()
	{
		MonoBehaviour.print(PlayerPrefs.GetInt("Game_PreviousVistedRoom", 0));
		int num = PlayerPrefs.GetInt("Game_PreviousVistedRoom", 0);
		Kris = PlayerManager.Instance;
		if (num == 51)
		{
			StartCoroutine(SkipCutscene());
			return;
		}
		LightworldMenu.Instance.CanOpenMenu = false;
		DarkworldMenu.Instance.CanOpenMenu = false;
		UI_FADE.Instance.StartFadeOut();
		Toriel.SetBool("InCutscene", value: true);
		Alphys.SetBool("InCutscene", value: true);
		Susie.SetBool("InCutscene", value: true);
		Berdly.SetBool("InCutscene", value: true);
		Kris.transform.position = new Vector2(11f, 3.8f);
		CameraManager.instance.transform.position = new Vector3(11f, 2.025f, -10f);
		Kris.PlayerSpriteRenderer.enabled = false;
		Kris._PMove.AnimationOverriden = true;
		Kris._PMove._anim.SetBool("MOVING", value: false);
		RotateAlphysToDirection(Vector2.down);
		RotateSusieToDirection(Vector2.down);
		RotateBerdlyToDirection(Vector2.down);
		RotateTorielToDirection(Vector2.right);
		StartCoroutine(Cutscene());
	}

	private IEnumerator Cutscene()
	{
		yield return new WaitForSeconds(1.5f);
		Kris._PMove.RotatePlayerAnim(Vector2.down);
		CutsceneUtils.PlaySound(CutsceneSounds[0]);
		Kris.PlayerSpriteRenderer.enabled = true;
		Kris._PMove._anim.SetBool("MOVING", value: true);
		Vector2 krisWalkPos = new Vector2(11f, -0.45f);
		yield return null;
		CutsceneUtils.MoveTransformLinear(Kris.transform, krisWalkPos, 2f);
		yield return new WaitForSeconds(1.5f);
		Susie.gameObject.SetActive(value: true);
		RotateSusieToDirection(Vector2.down);
		Susie.SetBool("InCutscene", value: true);
		Susie.Play("IdleSad");
		CutsceneUtils.PlaySound(CutsceneSounds[0]);
		yield return new WaitForSeconds(0.5f);
		Kris._PMove._anim.SetBool("MOVING", value: false);
		Kris._PMove.RotatePlayerAnim(Vector2.up);
		CutsceneUtils.RunFreshChat(CutsceneChats[0], 0, ForcePosition: true, OnBottom: true);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		CutsceneUtils.MoveTransformLinear(endPoint: new Vector2(11f, -1.3f), target: Susie.transform, duration: 1f);
		Susie.speed = 1.5f;
		Susie.Play("WalkSad");
		yield return new WaitForSeconds(0.66f);
		krisWalkPos = new Vector2(9.25f, -0.45f);
		Kris._PMove._anim.SetBool("MOVING", value: true);
		Kris._PMove.RotatePlayerAnim(Vector2.right);
		CutsceneUtils.MoveTransformLinear(Kris.transform, krisWalkPos, 0.33f);
		yield return new WaitForSeconds(0.33f);
		Kris._PMove._anim.SetBool("MOVING", value: false);
		Kris._PMove.RotatePlayerAnim(Vector2.right);
		RotateSusieToDirection(Vector2.left);
		Susie.Play("IdleSad");
		Susie.speed = 1f;
		CutsceneUtils.RunFreshChat(CutsceneChats[0], 1, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		yield return new WaitForSeconds(0.85f);
		CutsceneUtils.RunFreshChat(CutsceneChats[0], 2, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		yield return new WaitForSeconds(0.35f);
		Berdly.gameObject.SetActive(value: true);
		RotateBerdlyToDirection(Vector2.down);
		Berdly.SetBool("InCutscene", value: true);
		Berdly.speed = 2f;
		Berdly.Play("WalkN");
		CutsceneUtils.PlaySound(CutsceneSounds[0]);
		yield return null;
		CutsceneUtils.MoveTransformLinear(Berdly.transform, new Vector2(11f, -1.95f), 0.85f);
		yield return new WaitForSeconds(0.57f);
		CutsceneUtils.MoveTransformLinear(endPoint: new Vector2(9.25f, -1.75f), target: Kris.transform, duration: 0.28f);
		Vector2 vector = new Vector2(8.85f, -1.3f);
		Susie.Play("Susie_Shock_Right");
		CutsceneUtils.MoveTransformLinear(Susie.transform, vector, 0.28f);
		yield return new WaitForSeconds(0.28f);
		RotateBerdlyToDirection(Vector2.left);
		yield return new WaitForSeconds(0.33f);
		RotateBerdlyToDirection(Vector2.up);
		yield return new WaitForSeconds(0.33f);
		RotateBerdlyToDirection(Vector2.right);
		yield return new WaitForSeconds(0.66f);
		Berdly.Play("IdleN");
		Berdly.speed = 1f;
		yield return new WaitForSeconds(1f);
		RotateBerdlyToDirection(Vector2.left);
		RotateSusieToDirection(Vector2.right);
		BerdlySweatOverlay.SetActive(value: false);
		CutsceneUtils.RunFreshChat(CutsceneChats[0], 3, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		yield return new WaitForSeconds(1f);
		Toriel.gameObject.SetActive(value: true);
		RotateTorielToDirection(Vector2.down);
		Toriel.SetBool("InCutscene", value: true);
		Toriel.Play("TRBPhone_Idle");
		CutsceneUtils.PlaySound(CutsceneSounds[0]);
		BerdlyIdle();
		BerdlyUp();
		SusieUp();
		KrisUp();
		yield return new WaitForSeconds(1f);
		Toriel.Play("TRBPhone_Walk");
		RotateTorielToDirection(Vector2.down);
		CutsceneUtils.MoveTransformLinear(Toriel.transform, new Vector2(11f, 2.1f), 0.35f);
		yield return new WaitForSeconds(0.35f);
		RotateTorielToDirection(Vector2.left);
		CutsceneUtils.MoveTransformLinear(Toriel.transform, new Vector2(9.15f, 2.1f), 0.65f);
		yield return new WaitForSeconds(0.65f);
		Toriel.Play("TRBPhone_Idle");
		yield return new WaitForSeconds(0.35f);
		CutsceneUtils.RunFreshChat(CutsceneChats[0], 4, ForcePosition: true, OnBottom: true);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		yield return new WaitForSeconds(0.35f);
		CutsceneUtils.RunFreshChat(CutsceneChats[0], 5, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		yield return new WaitForSeconds(0.35f);
		CutsceneUtils.RunFreshChat(CutsceneChats[0], 6, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		yield return new WaitForSeconds(0.7f);
		CutsceneUtils.RunFreshChat(CutsceneChats[0], 7, ForcePosition: true, OnBottom: false);
		while (ChatboxManager.Instance.ChatIsCurrentlyRunning)
		{
			yield return null;
		}
		CutsceneUtils.MoveTransformLinear(CameraManager.instance.transform, new Vector3(9.25f, 2.025f, -10f), 1f);
		yield return new WaitForSeconds(1f);
		StartCoroutine(ConvertCutsceneCharactersToPartymembers());
		PlayerManager.Instance.ResetToGameState();
		CameraManager.instance.FollowPlayerX = true;
		LightworldMenu.Instance.CanOpenMenu = true;
	}

	private IEnumerator ConvertCutsceneCharactersToPartymembers(bool FadeOutWhenFinished = false)
	{
		PartyMemberSystem.Instance.AddPartyMember(PartyMemberSystem.Instance.Default_Susie);
		PartyMemberSystem.Instance.AddPartyMember(PartyMember_Berdly);
		while (PartyMemberSystem.Instance.HasMemberInParty(PartyMember_Berdly) == null && PartyMemberSystem.Instance.HasMemberInParty(PartyMemberSystem.Instance.Default_Susie) == null)
		{
			yield return null;
		}
		Susie.gameObject.SetActive(value: false);
		PartyMemberSystem.Instance.HasMemberInParty(PartyMemberSystem.Instance.Default_Susie).PartyMemberTransform.position = Susie.transform.position + new Vector3(0f, 0.9f, 0f);
		PartyMemberSystem.Instance.HasMemberInParty(PartyMemberSystem.Instance.Default_Susie).PartyMemberFollowSettings.SusieAnimator.Play("IdleSad");
		Berdly.gameObject.SetActive(value: false);
		PartyMemberSystem.Instance.HasMemberInParty(PartyMember_Berdly).PartyMemberTransform.position = Berdly.transform.position + new Vector3(0f, 0.9f, 0f);
		PartyMemberSystem.Instance.HasMemberInParty(PartyMember_Berdly).PartyMemberFollowSettings.SusieAnimator.Play("IdleN");
		PartyMemberSystem.Instance.AllPartyMemberFacePosition(PlayerManager.Instance.transform.position);
		yield return null;
		PartyMemberSystem.Instance.HasMemberInParty(PartyMember_Berdly).PartyMemberFollowSettings.AdjustForCutscene(InCutscene: false);
		PartyMemberSystem.Instance.HasMemberInParty(PartyMemberSystem.Instance.Default_Susie).PartyMemberFollowSettings.AdjustForCutscene(InCutscene: false);
		if (FadeOutWhenFinished)
		{
			UI_FADE.Instance.StartFadeOut();
		}
	}

	private IEnumerator SkipCutscene()
	{
		yield return null;
		LightworldMenu.Instance.CanOpenMenu = true;
		DarkworldMenu.Instance.CanOpenMenu = false;
		Susie.gameObject.SetActive(value: true);
		Berdly.gameObject.SetActive(value: true);
		Toriel.gameObject.SetActive(value: true);
		Toriel.SetBool("InCutscene", value: true);
		Toriel.transform.position = new Vector2(9.15f, 2.1f);
		TorielPhone_LookAway();
		Susie.transform.position = Kris.transform.position - new Vector3(0f, 0.9f, 0f);
		Berdly.transform.position = Kris.transform.position - new Vector3(0f, 0.9f, 0f);
		StartCoroutine(ConvertCutsceneCharactersToPartymembers(FadeOutWhenFinished: true));
		PartyMemberSystem.Instance.AllPartyMemberFaceDirection(Vector2.right);
		Kris.ResetToGameState();
	}

	private void RotateTorielToDirection(Vector2 direction)
	{
		CutsceneUtils.RotateCharacterToDirection(Toriel, "VelocityX", "VelocityY", direction);
	}

	private void RotateSusieToDirection(Vector2 direction)
	{
		CutsceneUtils.RotateCharacterToDirection(Susie, "VelocityX", "VelocityY", direction);
	}

	private void RotateBerdlyToDirection(Vector2 direction)
	{
		CutsceneUtils.RotateCharacterToDirection(Berdly, "VelocityX", "VelocityY", direction);
	}

	private void RotateAlphysToDirection(Vector2 direction)
	{
		CutsceneUtils.RotateCharacterToDirection(Alphys, "VelocityX", "VelocityY", direction);
	}

	private void RotateKrisToDirection(Vector2 direction)
	{
		PlayerManager.Instance._PMove.RotatePlayerAnim(direction);
	}

	public void SusieAngry()
	{
		Susie.Play("Susie_Angry_Left");
	}

	public void SusieAngry_Right()
	{
		Susie.Play("Susie_Angry_Right");
	}

	public void SusieIdle()
	{
		Susie.Play("Idle");
	}

	public void SusieAwkward()
	{
		Susie.Play("Susie_Awkward");
	}

	public void SusieLeft()
	{
		RotateSusieToDirection(Vector2.left);
	}

	public void SusieUp()
	{
		RotateSusieToDirection(Vector2.up);
	}

	public void SusieRight()
	{
		RotateSusieToDirection(Vector2.right);
	}

	public void SusieDown()
	{
		RotateSusieToDirection(Vector2.down);
	}

	public void SusieIdleSad()
	{
		Susie.Play("IdleSad");
	}

	public void SusieIdleShock_Right()
	{
		SusieRight();
		Susie.Play("IdleShock");
	}

	public void BerdlyLeft()
	{
		RotateBerdlyToDirection(Vector2.left);
	}

	public void BerdlyUp()
	{
		RotateBerdlyToDirection(Vector2.up);
	}

	public void BerdlyRight()
	{
		RotateBerdlyToDirection(Vector2.right);
	}

	public void BerdlyDown()
	{
		RotateBerdlyToDirection(Vector2.down);
	}

	public void BerdlySad()
	{
		Berdly.Play("BerdlyDownSad");
	}

	public void BerdlyShock_Left()
	{
		Berdly.Play("Shock");
	}

	public void BerdlyIdle()
	{
		Berdly.Play("IdleN");
	}

	public void BerdlyExclaim()
	{
		Berdly.Play("Exclaim");
	}

	public void KrisUp()
	{
		RotateKrisToDirection(Vector2.up);
	}

	public void KrisRight()
	{
		RotateKrisToDirection(Vector2.right);
	}

	public void TorielPhone_LookAway()
	{
		Toriel.Play("TRBPhone_LookAway");
	}
}
